//
//  UIButton+Additions.m
//  Together_iOS
//
//  Created by applex on 14-3-1.
//  Copyright (c) 2014年 QIU Shuang. All rights reserved.
//

#import "UIButton+Additions.h"

@implementation UIButton (Additions)

- (void)placeImageAboveText:(float)spacing {
    // @see http://stackoverflow.com/questions/2451223/uibutton-how-to-center-an-image-and-a-text-using-imageedgeinsets-and-titleedgei
    // spacing is the space between the image and text
    // get the size of the elements here for readability
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    
    // lower the text and push it left to center it
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);
    
    // the text width might have changed (in case it was shortened before due to
    // lack of space and isn't anymore now), so we get the frame size again
    titleSize = self.titleLabel.frame.size;
    
    // raise the image and push it right to center it
    self.imageEdgeInsets = UIEdgeInsetsMake(- (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);
}


@end
